package com.ruoyi.project.mgcmanage.controller;


import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mgcmanage.domain.Supplier;
import com.ruoyi.project.mgcmanage.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mgcmanage/supplier")
public class SupplierController extends BaseController {

    @Autowired
    private ISupplierService iSupplierService;

    /**
     * 分页查询供应商
     * @param supplier
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:supplier:query')")
    @GetMapping("/list")
    public TableDataInfo getList(Supplier supplier){
        startPage();
        return getDataTable(iSupplierService.selectAll(supplier));
    }

    /**
     * 添加供应商
     * @param supplier
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:supplier:add')")
    @PostMapping
    public AjaxResult addSupplier(@RequestBody Supplier supplier){
        return toAjax(iSupplierService.addSupplier(supplier));
    }

    /**
     * 数据回显
     * @param sid
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:supplier:query')")
    @GetMapping("/{sid}")
    public AjaxResult selectById(@PathVariable int sid){
        return AjaxResult.success(iSupplierService.selectById(sid));
    }

    /**
     * 修改供应商
     * @param supplier
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:supplier:edit')")
    @PutMapping
    public AjaxResult updateSupplier(@RequestBody Supplier supplier){
        return toAjax(iSupplierService.updateSupplier(supplier));
    }

    /**
     * 删除供应商
     * @param sids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:supplier:delete')")
    @DeleteMapping("/{sids}")
    public AjaxResult deleteByIds(@PathVariable int[] sids){
        return toAjax(iSupplierService.delete(sids));
    }

    /**
     * 查询所有供应商名称
     * @return
     */
    @PreAuthorize("@ss.hasPermi('mgcmanage:supplier:query')")
    @GetMapping("/all")
    public AjaxResult selectSuppliers(){
        return AjaxResult.success(iSupplierService.selectSupplierNames());
    }
}
